<?php

namespace app\admin\controller;

use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemDownload;
use think\admin\model\SystemSites;
use think\exception\HttpResponseException;

/**
 * 下载器配置
 * @class Download
 * @package app\admin\controller
 */
class Download extends Controller
{
    /**
     * 下载器配置
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        SystemDownload::mQuery()->layTable(function () {
            $this->title = '下载器配置';
        }, static function (QueryHelper $query) {
  
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$vo) try {
          $vo['type'] == 1 ? $vo['type'] = 'QB' : $vo['type'] = 'TR';
          $vo['is_skip_hash'] == 1 ? $vo['is_skip_hash'] = '跳过' : $vo['is_skip_hash'] = '不跳过';
          $vo['is_paused'] == 1 ? $vo['is_paused'] = '是' : $vo['is_paused'] = '否';
          $vo['reseed_sites_name'] = '';
        
          $siteIds = $array = explode(',', $vo['reseed_sites']);
          $sitesQuery = SystemSites::mQuery()->db();
          $siteNames = $sitesQuery->whereIn('id', $siteIds)->column('site_name');
          $vo['reseed_sites_name'] = implode(', ', $siteNames);
        } catch (\Exception $exception) {
        }
    }

    /**
     * 删除下载器
     * @auth true
     */
    public function remove()
    {
        SystemDownload::mDelete();
    }

    /**
     * 编辑下载器
     * @auth true
     */
    public function edit()
    {
        SystemDownload::mForm('edit');
    }
    /**
     * 添加下载器
     * @auth true
     */
    public function add()
    {
        SystemDownload::mForm('add');
    }
    /**
     * 下载器连接性测试
     * @auth true
     */
    public function connectCheck()
    {
      $id = $this->request->post('id');
      $downloadData = SystemDownload::mQuery()->db()->where('id',$id)->find();
      if($downloadData['type'] == 1){
        $qbApi = new Qbapi($downloadData->getData());
        if($qbApi->login_status){
          $this->success("Qb登录成功");
        }else{
          $this->error("Qb账号密码错误，如果错误3次以上会被Qb拉黑账号");
        }
      }else{
        $trApi = new Trapi($downloadData->getData());
        $result = json_decode($trApi->initSessionId()->getContent(),true);
        if($result['code']){
          $this->success( $result['msg']);
        }else{
          $this->error( $result['msg']);
        }
      }
    }
        /**
     * 删除所有下载器
     * @auth true
     */
    public function clear()
    {
        try {
            SystemDownload::mQuery()->empty();
            sysoplog('下载器管理', '成功删除所有下载器');
            $this->success('成功删除所有下载器！');
        } catch (HttpResponseException $exception) {
            throw $exception;
        }
    }
}
